
//
//  DCIMMapViewController.m
//  xmppBase
//
//  Created by striby on 16/1/29.
//  Copyright © 2016年 com.dc. All rights reserved.
//

#import "DCIMMapViewController.h"

@interface DCIMMapViewController ()
{
    BOOL isFirstLoaded;
    CLLocationCoordinate2D latestLocation;
    
    CLLocationManager * _locationManager;
    
    NSArray<CLPlacemark *> *  _placemarks;

}
@end

@implementation DCIMMapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    isFirstLoaded = YES;
    
    self.mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 64, DCDeviceWidth, DCDeviceHeight-64)];
    self.mapView.delegate=self;
    [self.view addSubview:self.mapView];
    
    
    
    self.mapView.showsUserLocation = YES;
    self.mapView.showsBuildings=YES;
    self.mapView.showsTraffic=YES;
    self.mapView.showsCompass=YES;
    self.mapView.showsPointsOfInterest=YES;
    self.mapView.showsScale=YES;
    
    //请求定位服务
    _locationManager=[[CLLocationManager alloc]init];
    if(![CLLocationManager locationServicesEnabled]||[CLLocationManager authorizationStatus]!=kCLAuthorizationStatusAuthorizedWhenInUse){
        [_locationManager requestWhenInUseAuthorization];
    }
    
   
    
    [self setRightItem];
}

#pragma mark - 右侧按钮
-(void)setRightItem
{
 
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(rightItemEvent)];
}

-(void)rightItemEvent
{
    
    if (isFirstLoaded==NO) {
        DDXMLElement * la = [[DDXMLElement alloc]initWithName:@"latitude" numberValue:[NSNumber numberWithDouble:self.mapView.centerCoordinate.latitude] ];
        
        DDXMLElement * lo = [[DDXMLElement alloc]initWithName:@"longitude" numberValue:[NSNumber numberWithDouble:self.mapView.centerCoordinate.longitude] ];
        
        
        if (_placemarks) {
            
            
            for (CLPlacemark *placemark in _placemarks) {
                //赋值详细地址
                //            self.textView.text=placemark.name;
                
                [DCMessageSender messageWithMessageType:self.conversationType==DCConversationTypeGroup?groupMessage:privateMessage andSubType:locationMessage forString:placemark.name addtionXMLs:@[la,lo] toUser:self.targetJID autoSendToServer:YES];
                break;
            }
            
            [self.navigationController popViewControllerAnimated:YES];
        }
        else
        {
            CLGeocoder *geocoder=[[CLGeocoder alloc]init];
            //创建位置
            CLLocation *location=[[CLLocation alloc]initWithLatitude:self.mapView.region.center.latitude longitude:self.mapView.region.center.longitude];
            
            
            
            //反地理编码
            [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
                //判断是否有错误或者placemarks是否为空
                if (error !=nil || placemarks.count==0) {
                    NSLog(@"%@",error);
                    return ;
                }
                for (CLPlacemark *placemark in placemarks) {
                    //赋值详细地址
                    //            self.textView.text=placemark.name;
                    
                    [DCMessageSender messageWithMessageType:self.conversationType==DCConversationTypeGroup?groupMessage:privateMessage andSubType:locationMessage forString:placemark.name addtionXMLs:@[la,lo] toUser:self.targetJID autoSendToServer:YES];
                    break;
                }
                
                [self.navigationController popViewControllerAnimated:YES];
            }];
        }
        
     

        
        
 

    }
    
    
}
#pragma mark - mapView delegate
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    
    latestLocation = userLocation.location.coordinate;
    
    if (isFirstLoaded) {
        float zoomValue = .001;
        
        MKCoordinateSpan theSpan;
        //    //地图的范围 越小越精确
        if (self.mapView.region.span.latitudeDelta<zoomValue) {
            theSpan.latitudeDelta=self.mapView.region.span.latitudeDelta;
            
        }
        else
        {
            theSpan.latitudeDelta=zoomValue;
            
        }
        
        if (self.mapView.region.span.longitudeDelta<zoomValue) {
            theSpan.longitudeDelta=self.mapView.region.span.longitudeDelta;
            
        }
        else
        {
            theSpan.longitudeDelta=zoomValue;
            
        }
        //
        MKCoordinateRegion theRegion;
        theRegion.center=[[userLocation location] coordinate];
        theRegion.span=theSpan;
        [self.mapView setRegion:theRegion];
        
        isFirstLoaded = NO;
        
        CLGeocoder *geocoder=[[CLGeocoder alloc]init];
        //创建位置
        CLLocation *location=[[CLLocation alloc]initWithLatitude:self.mapView.region.center.latitude longitude:self.mapView.region.center.longitude];
        
        
        
        //反地理编码
        [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
            //判断是否有错误或者placemarks是否为空
            if (error !=nil || placemarks.count==0) {
                NSLog(@"%@",error);
                return ;
            }
        
            _placemarks=placemarks;
            
            userLocation.title = [placemarks firstObject].name;
            
            
            
        }];

        
        
  
    }
  
    
    
    
    

}



@end
